#include "include.h"
#include "func.h"
#include "func_idle.h"
#include "api_spiflash_ex.h"
#include "func_exspiflash_music.h"

#if FUNC_IDLE_EN
#define MAX_FUNC_IDLE_TIMEOUT 30
#define MAX_MICON_IDLE_TIMEOUT 5*60
#define IDLE_POWER_ON_SONG_NUM 3
u16 func_timeout_cnt;
u16 max_timeout_time;
u8 long_wakeup=0;
AT(.text.func.idle)
void func_ilde_timeout(void)
{
	if(sys_cb.mic_onoff)
	{
		func_timeout_cnt=0;
		return;
	}
	if(sys_cb.power_on_song_play)
	{
		func_timeout_cnt=0;
		return;
	}
	func_timeout_cnt++;
	printf("idle cnt:%d\n",func_timeout_cnt);
	if(func_timeout_cnt>=max_timeout_time)
	{
		music_control(MUSIC_MSG_STOP);
		sys_cb.pwrdwn_tone_en=0;
		func_cb.sta=FUNC_PWROFF;
		func_timeout_cnt=0;
	}
}
AT(.text.func.idle)
void func_reset_timeout(void)
{
	func_timeout_cnt=0;
}
AT(.text.func.idle)
void func_idle_mic_onoff(u8 onoff)
{
	if(sys_cb.power_on_song_play)
	{
		func_timeout_cnt=0;
		return;
	}
	if(onoff)
	{
		if(sys_cb.vbat_low)
		{
			return;
		}
		printf("mic on!!\n");
		sys_cb.mic_onoff=1;
		bsp_set_led_mode(LED_SEQUENCE_7);
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		delay_ms(100);
		amp_speaker_unmute();
	}
	else
	{
		printf("mic off!!\n");
		sys_cb.mic_onoff=0;
		bsp_karaok_exit(AUDIO_PATH_KARAOK);
		bsp_set_led_mode(LED_SEQUENCE_2);
		amp_speaker_mute();
	}
}
AT(.text.func.idle)
void func_ilde_lower_power_check(void)
{
	if(sys_cb.mic_onoff&&sys_cb.karaok_init)
	 {
	 	u8 mic_off;
	 	mic_off=bsp_get_mic_vol_offset();
		if(sys_cb.mic_vol_offset!=mic_off)
		{
			sys_cb.mic_vol_offset=mic_off;
			bsp_karaok_set_mic_volume();
		}
		if ((sys_cb.vbat <=2700) ||(adc_cb.vrtc_val>=0x39f))
		 {
		 	sys_cb.vbat_low=1;
			bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 }
	 }
}
AT(.text.func.idle)
void func_QRcode_disable(void)
{
	if(sys_cb.project_num)
	{
		return;
	}
	param_QR_code_disable_read();
	if(sys_cb.QR_code_disable)
	{
		return;
	}
	sys_cb.QR_code_disable=1;
	param_QR_code_disable_write();
	param_sync();
	amp_speaker_unmute();
	mp3_res_play(RES_BUF_EN_POWERON_MP3, RES_LEN_EN_POWERON_MP3);
	amp_speaker_mute();
}

AT(.text.func.idle)
void func_ilde_reset(void)
{
	func_timeout_cnt=0;
}
AT(.text.func.idle)
void func_check_song_status(void)
{	
	if (get_music_dec_sta() == MUSIC_STOP)
	{
		if(!sys_cb.power_on_song_play)
		{
			return;
		}
		else
		{
			sys_cb.power_on_song_play=0;
			amp_speaker_mute();
			mic_bias_trim();
			bsp_set_led_mode(LED_SEQUENCE_2);
			func_timeout_cnt=0;
		}
	}
}
AT(.text.func.idle)
void func_idle_process(void)
{
    func_process();
	func_check_song_status();
}

static void func_idle_enter(void)
{
#if PLUGIN_FUNC_IDLE_ENTER_CHECK
    if (!plugin_func_idle_enter_check()) {  //可以处理开机, 判断是否要停在idle模式, 等待设备插入的方案.
        return;
    }
#endif
    led_idle();
	#if MIC_DETECT_EN
	if(!sys_cb.mic_onoff)
	{
		if(MIC_IS_ONLINE())
		{
			func_idle_mic_onoff(1);
		}
	}
	else
	{
		if(!MIC_IS_ONLINE())
		{
			func_idle_mic_onoff(0);
		}
	}
	#endif
	register_spi_read_function(spiflash1_read);
	func_cb.mp3_res_play = NULL;
	func_timeout_cnt=0;
	max_timeout_time=MAX_FUNC_IDLE_TIMEOUT;
	sys_cb.power_on_song_play=1;
	amp_speaker_unmute();
	bsp_set_led_mode(LED_SEQUENCE_1);
	exspifalsh_music_num_kick(IDLE_POWER_ON_SONG_NUM);
	//amp_speaker_mute();
	//bsp_set_led_mode(LED_SEQUENCE_2);
	func_timeout_cnt=0;
	max_timeout_time=MAX_FUNC_IDLE_TIMEOUT;
}

static void func_idle_exit(void)
{
	func_timeout_cnt=0;
	music_control(MUSIC_MSG_STOP);
	if(sys_cb.power_on_song_play)
	{
		sys_cb.power_on_song_play=0;
		mic_bias_trim();
	}
	register_spi_read_function(NULL);
    func_cb.last = FUNC_IDLE;
}
AT(.text.func.idle)
u8 check_power_key(void)
{
	u8 key_cnt=0;
	u16 msg;
	while (1)
	{
	    WDT_CLR();
	    delay_ms(20);
		msg = msg_dequeue();
		printf("msg:0x%x\n",msg);
		if(msg==KEY_POWER||msg==KL_POWER||msg==KH_POWER)
		{
			key_cnt=10;
			break;
		}
		key_cnt++;
		if(key_cnt>6)
		{
			break;
		}
	}
	if(key_cnt>=10)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

AT(.text.func.idle)
void func_idle(void)
{
    printf("%s\n", __func__);
	if(check_power_key()==0)
	{
		printf("key long!!\n");
		long_wakeup=1;
	}
    func_idle_enter();

    while (func_cb.sta == FUNC_IDLE) {
        func_idle_process();
        func_idle_message(msg_dequeue());
        func_idle_display();
    }

    func_idle_exit();
}

#endif
